import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import autoprefixer from 'autoprefixer'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  server: {
    port: 3000,
    host: '0.0.0.0',
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8088',
        changeOrigin: true,
        secure: false,
        ws: true,
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.error('Proxy Error:', err)
            if (res) {
              res.writeHead(500, {
                'Content-Type': 'text/plain',
              })
              res.end('Proxy Error: ' + err.message)
            }
          })
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('Proxy Request:', {
              method: req.method,
              url: req.url,
              headers: req.headers,
              target: 'http://127.0.0.1:8088'
            })
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('Proxy Response:', {
              statusCode: proxyRes.statusCode,
              statusMessage: proxyRes.statusMessage,
              headers: proxyRes.headers,
              url: req.url
            })
          })
        }
      }
    }
  },
  css: {
    postcss: {
      plugins: [autoprefixer]
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    sourcemap: false,
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  }
})
